package com.scut.online_black_market.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scut.online_black_market.dto.ErrandResponse;
import com.scut.online_black_market.dto.PostErrandInfo;
import com.scut.online_black_market.dto.ShoppingGuideResponse;
import com.scut.online_black_market.entity.ErrandTask;
import com.scut.online_black_market.entity.Personal;
import com.scut.online_black_market.exception.AskForProductException;
import com.scut.online_black_market.exception.PostErrandException;
import com.scut.online_black_market.exception.UniversalException;
import com.scut.online_black_market.mapper.ErrandTaskMapper;
import com.scut.online_black_market.entity.ErrandOrder;
import com.scut.online_black_market.mapper.ErrandOrderMapper;
import com.scut.online_black_market.mapper.PersonalMapper;
import com.scut.online_black_market.service.ErrandService;
import com.scut.online_black_market.utility.JwtUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.time.LocalDateTime;

@Service
public class ErrandServiceImpl implements ErrandService {

    @Resource
    private ErrandTaskMapper errandTaskMapper;

    @Resource
    private JwtUtil jwtUtil;

    @Resource
    private HttpServletRequest httpServletRequest;

    @Resource
    private ErrandOrderMapper errandOrderMapper;

    @Resource
    private PersonalMapper personalMapper;


    public void postErrand(PostErrandInfo postErrandInfo)throws PostErrandException {
        String studentNumber=jwtUtil.extractStudentNumber(httpServletRequest);

        Personal personal= personalMapper.selectById(studentNumber);

        if (personal == null) {
            throw new PostErrandException("用户不存在");
        }

        if (personal.getBalance()-postErrandInfo.getPrice() >= 0)
            personal.setBalance(personal.getBalance()-postErrandInfo.getPrice());

        ErrandTask errandTask=ErrandTask.builder()
                .proposerNumber(postErrandInfo.getProposerNumber())
                .startPlace(postErrandInfo.getStartPlace())
                .endPlace(postErrandInfo.getEndPlace())
                .weight(postErrandInfo.getWeight())
                .startTime(LocalDateTime.now())
                .endTime(LocalDateTime.now())
                .price(postErrandInfo.getPrice())
                .note(postErrandInfo.getNote())
                .state(1)
                .build();

        if(errandTaskMapper.insert(errandTask)!=1||personalMapper.updateById(personal)!=1){
            throw new PostErrandException("发布代跑腿失败");
        }
    }


    @Override
    public void accept(BigInteger errandID, String acceptor) throws UniversalException {

        String studentNumber = jwtUtil.extractStudentNumber(httpServletRequest);

        //判断登录的用户跟接单用户是否一致
        if (!studentNumber.equals(acceptor)) {
            throw new UniversalException("学号错误");
        }

        ErrandTask errandTask = errandTaskMapper.selectById(errandID);
        //判断跑腿任务是否存在且状态为待接受
        if (errandTask == null || errandTask.getState() == 0) {
            throw new UniversalException("该跑腿任务不存在");
        }
        //改变跑腿任务状态为已被接受
        errandTask.setState(0);
        //构建订单
        ErrandOrder errandOrder = ErrandOrder.builder()
                .errandID(errandID)
                .errandState(1)
                .providerNumber(studentNumber)
                .proposerNumber(errandTask.getProposerNumber())
                .build();

        //判断数据库操作失败异常
        if (errandTaskMapper.updateById(errandTask) != 1 || errandOrderMapper.insert(errandOrder) != 1){
            throw new UniversalException("接受跑腿任务失败，请重试!");
        }

    }
    @Override
    public ErrandResponse queryByErrandId(BigInteger errandId)throws UniversalException {
        ErrandTask task = errandTaskMapper.selectById(errandId);

        if(task ==null){
            throw new UniversalException("该跑腿订单不存在");
        }

        ErrandResponse errandResponse=ErrandResponse.builder()
                .note(task.getNote())
                .build();

        return errandResponse;
    }


    @Override
    public IPage<ErrandResponse> queryErrandByPage(short pageNo, short pageSize, short typeId) {

        IPage<ShoppingGuideResponse> page= new Page<>(pageNo,pageSize);

        switch (typeId)
        {
            case 2:
                return errandTaskMapper.selectType2(page);
            case 3:
                return errandTaskMapper.selectType3(page);
            case 4:
                return errandTaskMapper.selectType4(page);
            case 5:
                return errandTaskMapper.selectType5(page);
            default:
                return errandTaskMapper.selectType1(page);
        }
    }
}
